package com.ruoyi.system.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.system.domain.BdmBalanceChange;
import com.ruoyi.system.domain.dto.BdmBalanceChangeDto;
import com.ruoyi.system.domain.dto.BdmQueryCommissionDto;
import com.ruoyi.system.domain.vo.*;
import com.ruoyi.system.domain.vo.applet.BdmBalanceChangeAmountVo;
import com.ruoyi.system.enums.BdmBalanceChangeEnum;
import com.ruoyi.system.mapper.BdmBalanceChangeMapper;
import com.ruoyi.system.service.IBdmBalanceChangeService;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * 用户余额积分佣金变动Service业务层处理
 *
 * @author ruoyi
 * @date 2024-12-19
 */
@Service
public class BdmBalanceChangeServiceImpl extends ServiceImpl<BdmBalanceChangeMapper, BdmBalanceChange> implements IBdmBalanceChangeService {
    @Autowired
    private BdmBalanceChangeMapper bdmBalanceChangeMapper;

    /**
     * 查询用户余额变动
     *
     * @param id 用户余额变动主键
     * @return 用户余额变动
     */
    @Override
    public BdmBalanceChange selectBdmBalanceChangeById(Long id) {
        return bdmBalanceChangeMapper.selectBdmBalanceChangeById(id);
    }

    /**
     * 查询用户余额变动列表
     *
     * @param bdmBalanceChange 用户余额变动
     * @return 用户余额变动
     */
    @Override
    public List<BdmBalanceChange> selectBdmBalanceChangeList(BdmBalanceChange bdmBalanceChange) {
        return bdmBalanceChangeMapper.selectBdmBalanceChangeList(bdmBalanceChange);
    }

    /**
     * 新增用户余额变动
     *
     * @param bdmBalanceChange 用户余额变动
     * @return 结果
     */
    @Override
    public int insertBdmBalanceChange(BdmBalanceChange bdmBalanceChange) {
        bdmBalanceChange.setCreateTime(DateUtils.getNowDate());
        return bdmBalanceChangeMapper.insertBdmBalanceChange(bdmBalanceChange);
    }

    /**
     * 修改用户余额变动
     *
     * @param bdmBalanceChange 用户余额变动
     * @return 结果
     */
    @Override
    public int updateBdmBalanceChange(BdmBalanceChange bdmBalanceChange) {
        bdmBalanceChange.setUpdateTime(DateUtils.getNowDate());
        return bdmBalanceChangeMapper.updateBdmBalanceChange(bdmBalanceChange);
    }

    /**
     * 批量删除用户余额变动
     *
     * @param ids 需要删除的用户余额变动主键
     * @return 结果
     */
    @Override
    public int deleteBdmBalanceChangeByIds(Long[] ids) {
        return bdmBalanceChangeMapper.deleteBdmBalanceChangeByIds(ids);
    }

    /**
     * 删除用户余额变动信息
     *
     * @param id 用户余额变动主键
     * @return 结果
     */
    @Override
    public int deleteBdmBalanceChangeById(Long id) {
        return bdmBalanceChangeMapper.deleteBdmBalanceChangeById(id);
    }

    /**
     * 分页 查询 用户 余额 积分 佣金变动列表
     */

    @Override
    public List<BdmBalanceChange> listPage(BdmBalanceChangeDto bdmBalanceChangeDto) {
        if (ObjectUtils.isNotEmpty(bdmBalanceChangeDto.getStartTime())&&ObjectUtils.isNotEmpty(bdmBalanceChangeDto.getEndTime())){
            bdmBalanceChangeDto.setStartTime(bdmBalanceChangeDto.getStartTime()+" 00:00:00");
            bdmBalanceChangeDto.setEndTime(bdmBalanceChangeDto.getEndTime()+" 23:59:59");
        }
        return bdmBalanceChangeMapper.listPage(bdmBalanceChangeDto);

    }

    @Override
    public List<BdmBalanceChangeVo> listMemberAsset(BdmBalanceChangeDto bdmBalanceChangeDto) {
        return bdmBalanceChangeMapper.listMemberAsset(bdmBalanceChangeDto);
    }

    /**
     * 统计余额
     */
    @Override
    public BdmStatisticalBalanceVo statisticalBalance() {

        return bdmBalanceChangeMapper.statisticalBalance();
    }

    /**
     * 统计积分
     */

    @Override
    public BdmStatisticalIntegralVo statisticalIntegral() {

        return bdmBalanceChangeMapper.statisticalIntegral();
    }

    /**
     * 统计佣金
     */
    @Override
    public BdmStatisticalBrokerageVo statisticalBrokerage() {
        return bdmBalanceChangeMapper.statisticalBrokerage();
    }

    /**
     * 查询  佣金
     */
    @Override
    public List<BdmBalanceChange> listCommission(BdmQueryCommissionDto bdmQueryCommissionDto) {
        return bdmBalanceChangeMapper.bdmQueryCommissionDto(bdmQueryCommissionDto);
    }

    /**
     * 查询会员账户资产
     */
    @Override
    public BdmMemberAccountAssetsVo queryMemberAccountAssets() {

        return bdmBalanceChangeMapper.queryMemberAccountAssets();
    }

    /**
     * 查询 订单概况 支付统计
     */
    @Override
    public BdmOrderProfileAndPaymentStatisticsVo queryOrderProfileAndPaymentStatistics(String date) {
               String startTime = date + "-01";
               // 指定日期字符串
               SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
               Date thisDate = null;
               try {
                   thisDate = sdf.parse(startTime);
               } catch (ParseException e) {
                   e.printStackTrace();
               }

       // 获取Calendar实例并设置时间
               Calendar cld = Calendar.getInstance();
               cld.setTime(thisDate);

       // 月份加1，天设置为0
               cld.add(Calendar.MONTH, 1);
               cld.set(Calendar.DAY_OF_MONTH, 0);

       // 获取最后一天
               String endTime = sdf.format(cld.getTime());

        return bdmBalanceChangeMapper.queryOrderProfileAndPaymentStatistics(startTime,endTime);
    }

    /**
     * 查询 收支趋势 type 1 近七天  2 近一个月
     */
    @Override
    public List<BdmRevenueAndExpenditureTrendVo> queryRevenueAndExpenditureTrend(Long type) {
        return bdmBalanceChangeMapper.queryRevenueAndExpenditureTrend(type);
    }

    /**
     * 查询业务类型
     */
    @Override
    public List<String> queryBusinessType() {
        return bdmBalanceChangeMapper.queryBusinessType();
    }

    /**
     * 分页查询当前用户余额支出明细
     * @return
     */
    @Override
    public List<BdmBalanceChange> expenditurePage(Long type) {
        BdmBalanceChange bdmBalanceChange = new BdmBalanceChange();
        bdmBalanceChange.setAddOrSubtract(type);
        bdmBalanceChange.setUserId(SecurityUtils.getUserId());
        List<BdmBalanceChange> list = bdmBalanceChangeMapper.selectBdmBalanceChangeList(bdmBalanceChange);
        for(BdmBalanceChange bdmBalanceChange1:list){
            if(BdmBalanceChangeEnum.LOWER_LEVEL_PURCHASES_OF_CLOUD_INVENTORY_ITEMS.getDescription().equals(bdmBalanceChange1.getBusinessType())){
                if(("1").equals(bdmBalanceChange1.getBusinessType())){
                    bdmBalanceChange1.setBusinessType(bdmBalanceChange1.getBusinessType()+"(余额)");
                }
                if(("3").equals(bdmBalanceChange1.getBusinessType())){
                    bdmBalanceChange1.setBusinessType(bdmBalanceChange1.getBusinessType()+"(佣金)");
                }
                if(("2").equals(bdmBalanceChange1.getBusinessType())){
                    bdmBalanceChange1.setBusinessType(bdmBalanceChange1.getBusinessType()+"(积分)");
                }
            }
        }
        return list;
       // return this.lambdaQuery().eq(BdmBalanceChange::getAddOrSubtract,type).eq(BdmBalanceChange::getUserId, SecurityUtils.getUserId()).orderByDesc(BdmBalanceChange::getCreateTime).list();
    }

    @Override
    public List<BdmBalanceChangeAmountVo> getExpenditurePage(Long addOrSubtract) {
        return bdmBalanceChangeMapper.getExpenditurePage(addOrSubtract, SecurityUtils.getUserId());
    }



}
